package pers.jason.wppm.auth.web.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.validation.annotation.Validated;
import pers.jason.wppm.api.entity.PageSupport;
import pers.jason.wppm.api.entity.auth.User;
import pers.jason.wppm.api.support.ResultView;
import pers.jason.wppm.auth.api.dto.*;
import pers.jason.wppm.auth.api.enums.LoginType;
import pers.jason.wppm.auth.web.dao.UserDao;
import pers.jason.wppm.auth.web.service.UserService;


/**
 * 
 * @author Jason
 * @Date 2018-10-25 14:40
 *
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	/**
	 * 根据登录名获取用户信息
	 */
	@Override
	public ResultView findUserForLogin(AuthenticationCondition authenticationCondition) {
		// TODO Auto-generated method stub
		if(LoginType.LOGIN_BY_EMAIL.getName().equals(authenticationCondition.getLoginType()) && StringUtils.isEmpty(authenticationCondition.getEmail())) {
			throw new RuntimeException("邮箱不能为空");
		}
		if(LoginType.LOGIN_BY_FORM.getName().equals(authenticationCondition.getLoginType()) && StringUtils.isEmpty(authenticationCondition.getLoginName())) {
			throw new RuntimeException("登录名不能为空");
		}
		if(LoginType.LOGIN_BY_MOBILE.getName().equals(authenticationCondition.getLoginType()) && StringUtils.isEmpty(authenticationCondition.getMobile())) {
			throw new RuntimeException("手机号不能为空");
		}
		User user = userDao.findUserForLogin(authenticationCondition);
		if(null != user) {
			return ResultView.success(user);
		}
		return ResultView.failure();
	}

	/**
	 * 根据id查询用户详细信息
	 */
	@Override
	public ResultView getUserInfoById(String id) {
		// TODO Auto-generated method stub
		User user = userDao.getUserInfoById(id);
		if(null != user) {
			return ResultView.success(user);
		}
		return ResultView.failure();
	}

	/**
	 * 条件查询用户列表
	 */
	@Override
	public ResultView queryUserList(UserQueryConditionPageable userQueryConditionPageable) {
		// TODO Auto-generated method stub
		UserQueryCondition user = userQueryConditionPageable.getUserQueryCondition();
		Integer page = userQueryConditionPageable.getPage();
		Integer rows = userQueryConditionPageable.getRows();
		String sortColumn = userQueryConditionPageable.getSortColumn();
		PageSupport.Sort sort = userQueryConditionPageable.getSort();
		List<User> users = userDao.queryUserList(page, rows, sortColumn, sort.getName(), user);
		ResultView resultView = ResultView.success(users);
		return resultView;
	}

	/**
	 * 修改用户基本信息（不包含登录名和密码）
	 */
	@Override
	@Transactional
	public ResultView updateUserInfo(UserUpdateCondition userUpdateCondition) {
		// TODO Auto-generated method stub
		int result = userDao.updateUserInfo(userUpdateCondition);
		if(1 == result) {
			return ResultView.success();
		}
		return ResultView.failure();
	}

	/**
	 * 删除用户
	 */
	@Override
	@Transactional
	public ResultView deleteUser(String id) {
		// TODO Auto-generated method stub
		int result = userDao.deleteUser(id);
		if(1 == result) {
			return ResultView.success();
		}
		return ResultView.failure();
	}

	/**
	 * 新增用户
	 * @param userInsertCondition
	 * @return
	 */
	@Override
	@Transactional
	public ResultView registUserByMobile(@Validated UserInsertCondition userInsertCondition) {
		int result = userDao.registUserByMobile(userInsertCondition);
		if(1 == result) {
			User user = new User();
			BeanUtils.copyProperties(userInsertCondition, user);
			return ResultView.success(user);
		}
		return ResultView.failure();
	}

}
